-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overload get() function for Optional
type.
#9748
Conversation
Why close this PR? I'm very interested |
CI somehow failed, I'm trying to figure it out.
…On Wed, Dec 15, 2021, 1:49 AM Junru Shao ***@***.***> wrote:
Why close this PR? I'm very interested
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#9748 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACZ2NM3O2FN62RIBIOPSPV3URBQELANCNFSM5KDBWGQA>
.
|
I think the CI was broken because of some random numerical issue and I'll re-trigger it, @junrushao1994 any further comments on this PR? |
Yes, it's a bit of flaky. Let me retrigger this |
@yzh119 Would you like to rebase and retrigger the CI? Thanks! |
@junrushao1994 done. |
* \return The internal object pointer with container type of T. | ||
* \note This function do not perform not-null checking. | ||
*/ | ||
const ContainerType* get() const { return static_cast<ContainerType*>(data_.get()); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that get is not a standard API as per stl style for Optional. While get
is a standard stl style name. We might want to have a longer name in this case.
How about GetContainerPtr
(use CamelCase to indicate that it is not STL compatible).
The comment can be updated to explain the behavior of nullopt case(return nullptr) and non-nullopt case(returns the container ptr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tqchen for the valuable feedback! On the other hand, I would say this PR actually does is to simply improve the Optional::get
method to return the underlying type instead of the plain Object pointer, and hence it might make sense to keep the name as it is. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I didn't noticed that ObjectRef also have a get method. I think this sounds good to me then. Perhaps we can just add additional comments about the behavior
* upd * simplify * upd * fix * upd * fix docstring
* upd * simplify * upd * fix * upd * fix docstring
Currently
Optional
don't have its ownget()
implementation, so if we want to get the internal container pointer of an optional objecto
with typeOptional<T>
, we can either useo.value().get()
(in the case it's defined) orstatic_cast<const T*>(o.get())
(because the inheritedget()
function has no type information). Neither of them are satisfactory if we want to get a pointer with typeconst T*
.This PR overloads the
get()
function ofOptional
, which returnsnullptr
when the object is not defined, and it's corresponding data pointer otherwise.cc @junrushao1994 @tqchen